package com.ghh.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.ghh.constant.MessageConstant;
import com.ghh.entity.PageResult;
import com.ghh.entity.QueryPageBean;
import com.ghh.entity.Result;
import com.ghh.pojo.Setmeal;
import com.ghh.service.SetmealService;
import com.ghh.utils.QiniuUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;


    //图片上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {

        try {
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), uuid);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, uuid);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    //添加套餐
    @PreAuthorize("hasAuthority('SETMEAL_ADD')")
    @RequestMapping("/addSetmeal")
    public Result addSetmeal(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        try {
            setmealService.addSetmeal(setmeal, checkgroupIds);
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    //分页查询
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return setmealService.pageQuery(queryPageBean);
    }


    //查找所有的套餐
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Setmeal> list = setmealService.getSetmeal();
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

}
